import 'dart:math';

import 'package:flutter/material.dart';
import 'package:giffy_dialog/giffy_dialog.dart';
import 'dart:async';

import 'package:lucky_guy/global.dart';
import 'package:lucky_guy/main_manager.dart';
import 'package:provider/provider.dart';

class RandomPage extends StatefulWidget {
  @override
  _RandomPageState createState() => _RandomPageState();
}

class _RandomPageState extends State<RandomPage> {
  Timer _timer;
  int _currentTime = 0;

  String _text = '点击开始';

  bool _drawing = false;

  @override
  void initState() {
    super.initState();
  }

  periodic(context, _mainManager) {
    _currentTime = 0;

    setState(() {
      _drawing = true;
    });
    _timer = Timer.periodic(Duration(milliseconds: 50), (timer) {
      setState(() {
        _currentTime += 50;
        _text = _mainManager
            .allLuckyGuys[Random().nextInt(_mainManager.allLuckyGuys.length)];
      });
      if (_currentTime >= 3000) {
        _timer.cancel();
        setState(() {
          _drawing = false;
        });
        showDialog(
          context: context,
          builder: (_) => NetworkGiffyDialog(
            image: Image.asset(
              "assets/images/asa.png",
              fit: BoxFit.cover,
            ),
            entryAnimation: EntryAnimation.BOTTOM,
            title: Text(
              '为 $_text 成为新的父亲干杯！',
              textAlign: TextAlign.center,
              style: TextStyle(fontSize: 22.0, fontWeight: FontWeight.w600),
            ),
            onlyCancelButton: true,
            buttonCancelText: Text(
              '不服！再来一次',
              textAlign: TextAlign.center,
              style: TextStyle(
                  color: Global.backgroundColor,
                  fontSize: 15,
                  fontWeight: FontWeight.w600),
            ),
          ),
        );
      }
    });
  }

  @override
  void dispose() {
    if (_timer != null) {
      _timer.cancel();
    }
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    MainManager _mainManager = Provider.of<MainManager>(context);
    return Scaffold(
      body: Container(
        padding: EdgeInsets.all(20),
        decoration: BoxDecoration(
          gradient: LinearGradient(
            begin: Alignment.topCenter,
            end: Alignment.bottomCenter,
            colors: [
              Color(0xFF919191),
              Color(0xFFE8E7ED),
            ],
          ),
        ),
        width: MediaQuery.of(context).size.width,
        height: MediaQuery.of(context).size.height,
        child: Column(
          children: [
            SizedBox(height: Global.paddingTop),
            Row(
              children: [
                InkWell(
                  onTap: () {
                    Navigator.of(context).pop();
                  },
                  child: Container(
                    child: Row(
                      children: [
                        Icon(
                          Icons.arrow_back_ios,
                          size: 15,
                          color: Global.backgroundColor,
                        ),
                        Text(
                          '返回',
                          style: TextStyle(
                            color: Global.backgroundColor,
                            fontSize: 14,
                            fontWeight: FontWeight.w600,
                          ),
                        ),
                      ],
                    ),
                  ),
                ),
                Expanded(
                  child: Center(
                    child: Text(
                      '',
                      style: TextStyle(
                        color: Global.backgroundColor,
                        fontSize: 16,
                        fontWeight: FontWeight.w600,
                      ),
                    ),
                  ),
                ),
                SizedBox(width: 43),
              ],
            ),
            Expanded(
              child: Stack(
                children: [
                  Positioned(
                    top: 60,
                    left: 0,
                    child: Container(
                      width: MediaQuery.of(context).size.width - 40,
                      height: (MediaQuery.of(context).size.width - 40) / 1.2,
                      decoration: BoxDecoration(
                        image: DecorationImage(
                          image: AssetImage("assets/images/laohuji1.png"),
                          repeat: ImageRepeat.repeat,
                          fit: BoxFit.fill,
                        ),
                      ),
                    ),
                  ),
                  Positioned(
                    top: 60,
                    left: 0,
                    child: Container(
                      width: MediaQuery.of(context).size.width - 40,
                      height: (MediaQuery.of(context).size.width - 40) / 1.2,
                      child: Center(
                        child: Text(
                          _text,
                          style: TextStyle(
                            color: Color(0xFFA5320F),
                            fontSize: 45,
                            fontWeight: FontWeight.w600,
                          ),
                        ),
                      ),
                    ),
                  ),
                  Positioned(
                    top: 400,
                    left: 80,
                    right: 80,
                    child: InkWell(
                      onTap: () {
                        if (!_drawing) {
                          periodic(context, _mainManager);
                        }
                      },
                      child: Container(
                        padding: EdgeInsets.fromLTRB(45, 15, 45, 15),
                        decoration: BoxDecoration(
                          gradient: LinearGradient(
                            begin: Alignment.topLeft,
                            end: Alignment.bottomRight,
                            colors: [
                              Color(0xFF6C6C6C),
                              Color(0xFF292929),
                            ],
                          ),
                          borderRadius: BorderRadius.circular(20),
                        ),
                        child: Center(
                          child: Text(
                            '开始',
                            style: TextStyle(
                              color: Global.backgroundColor,
                              fontSize: 27,
                              fontWeight: FontWeight.w600,
                            ),
                          ),
                        ),
                      ),
                    ),
                  )
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}
